#-*- coding:utf-8 -*-
import re
import random
import urllib2
import bson
import json
from steam_data_collect.mongodb_models import proxy_ips
import logging
loger=logging.getLogger(__file__)

def filter_words(content=None):
    """
    去除字符串开头和结尾特定字符
    :param content: 文本
    :return: 处理后的文本
    """
    if isinstance(content,basestring)==False or content.__len__()<1:
        return ""
    words=["\r","\n","\t"]
    start_count=0
    for i in range(content.__len__()):
        if content[i] not in words:
            start_count=i
            break
    temp_content=content[start_count:content.__len__()-1]

    if temp_content.__len__()<1:
        return temp_content

    temp_list=list(temp_content)
    temp_list.reverse()
    temp_content=u"".join(temp_list)

    start_count = 0
    for i in range(temp_content.__len__()):
        if temp_content[i] not in words:
            start_count = i
            break
    if temp_content.__len__()<1:
        return temp_content

    temp_content = temp_content[start_count:temp_content.__len__() - 1]
    temp_list = list(temp_content)
    temp_list.reverse()
    temp_content = u"".join(temp_list)
    return temp_content

def filter_repeats(content=None):
    """
    把字符串中连续重复的字符替换成一个字符
    :param content: 文本
    :return: 处理后的文本
    """
    if isinstance(content,basestring)==False or content.__len__()<1:
        return ""
    words=["\r","\n","\t"]

    for word in words:
        re_arg=r'{}+'.format(word)
        content=re.sub(re_arg,word,content)
    return content




class ip_handles(object):
    """
    获取一个代理ip port
    """
    def __init__(self):
        self.proxy_ips_json=json.loads(proxy_ips.objects.all().to_json())

    def _check_ip(self,ip=None):
        """
        检测代理ip是否可以用
        :param ip: ip信息 type是一个list
        :return: type bool
        """
        if isinstance(ip,list)==False or ip.__len__()==0:
            return False
        http_url = "http://www.kaoala.com/"
        https_url = "https://www.baidu.com/"

        url = http_url if ip[3] == "http" else https_url
        proxy = "%s:%s" % (ip[1], ip[2])
        try:
            req = urllib2.Request(url=url)
            req.set_proxy(proxy, ip[3].lower())
            response = urllib2.urlopen(req, timeout=30)
        except Exception, e:
            loger.error(e)
            loger.warning(u"ip代理失效 {}".format(ip))
            return False
        else:
            code = response.getcode()
            if code >= 200 and code < 300:
                loger.info(u"ip代理可以使用 {}".format(ip))
                return True
            else:
                loger.warning(u"ip代理失效 {}".format(ip))
                return False


    def _random_ip(self):
        """
        随机选择一个ip记录
        :return:
        """
        if self.proxy_ips_json.__len__()==0:
            return []
        index_list=range(self.proxy_ips_json.__len__())
        index=random.choice(index_list)
        ip_item=self.proxy_ips_json.pop(index)
        id=ip_item["_id"].values()[0]
        ip=ip_item["ip"]
        port=ip_item["port"]
        qtype=ip_item["type"]
        loger.info(u"选取一个代理 {}".format([id,ip,port,qtype]))
        return [id,ip,port,qtype]

    def _delete_ip_record(self,ip=None):
        """
        删除失效的ip记录
        :param ip: ip信息 type是一个list
        :return: type bool
        """
        if isinstance(ip,list)==False or ip.__len__()==0:
            return False
        proxy_objs=proxy_ips.objects.filter(id=bson.ObjectId(ip[0]))
        if proxy_objs.count()>0:
            proxy_objs.delete()
            loger.info(u"删除一个代理 {}".format(ip))
            return True
        else:
            return False


    def get_one_ip_port(self):
        while True:
            if self.proxy_ips_json.__len__()<1:
                return None,None
            one_ip=self._random_ip()
            if self._check_ip(one_ip)==False:
                self._delete_ip_record(one_ip)
                continue
            else:
                ip,port=one_ip[1],one_ip[2]
                return ip,port




if __name__ == '__main__':
    pass